package Classic150;

import java.util.ArrayList;
import java.util.List;

public class 插入区间 {
    /*
        分阶段考虑
     */
    public int[][] insert(int[][] intervals, int[] newInterval) {
        List<int[]> list = new ArrayList<>();
        int i = 0;
        for (; i < intervals.length && intervals[i][1] < newInterval[0]; i++) list.add(intervals[i]);
        for (; i < intervals.length && intervals[i][0] <= newInterval[1]; i++) {
            newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
            newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
        }
        list.add(newInterval);
        for (; i < intervals.length && intervals[i][0] > newInterval[1]; i++) list.add(intervals[i]);
        int[][] res = new int[list.size()][2];
        for (int j = 0; j < res.length; j++) res[j] = list.get(j);
        return res;
    }
}
